<extend name='Layout/ins_page' />
<block name="content">
	<div class="popup-box">
		{:W('PageHeader',array('name'=>'写消息','search'=>'N'))}
		<form class="well form-horizontal" method='post' id="form_data" name="form_data" enctype="multipart/form-data">
			<input type="hidden" id="ajax" name="ajax" value="0">
			<input type="hidden" id="to" name="to"/>
			<input type="hidden" id="opmode" name="opmode" value="add"/>
			<input type="text" style="display:none">

			<div class="form-group">
				<label class="col-sm-2 control-label" for="recever">收信人*：</label>
				<div class="col-sm-10">
					<div id="recever" class="inputbox">
						<a class="pull-right btn btn-link text-center" onclick="popup_contact();"> <i class="fa fa-user"></i> </a>
						<div class="wrap" >
							<span class="address_list"></span>
							<span class="text" >
								<input class="letter" type="text"  >
							</span>
						</div>
						<div class="search dropdown ">
							<ul class="dropdown-menu"></ul>
						</div>
					</div>
				</div>
			</div>
			<div class="form-group">
				<div class="col-xs-12">
					<textarea  class="simple" id="content" name="content" class="col-xs-12" check="require" msg="请输入内容"></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" for="name">附件：</label>
				<div class="col-sm-10">
					{:W('FileUpload/add')}
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-10 col-sm-offset-2">
					<a onclick="save();"  class="btn btn-sm btn-primary">发送</a>
					<a onclick="go_return_url();" class="btn btn-sm btn-default">取消</a>
				</div>
			</div>
		</form>
	</div>
</block>

<block name="js">
	<script type="text/javascript">
		$(document).ready(function() {

			/*单击删除已选联系人*/
			$(document).on("click", ".inputbox .address_list a.del", function() {
				$(this).parent().parent().remove();
			});

			/* 查找联系人input 功能*/
			$(document).on("click", ".inputbox .search li", function() {
				name = $(this).text().replace(/<.*>/, '');
				data = $(this).find("a").attr("data");
				html = conv_inputbox_item(name, data);

				inputbox = $(this).parents(".inputbox");
				inputbox.find("span.address_list").append(html);
				inputbox.find("input.letter").val("");
				inputbox.find(".search ul").html("");
				inputbox.find(".search ul").hide();
				inputbox.find(".search").hide();
			});
			/* 查找联系人input 功能*/
			$(".inputbox .letter").keyup(function(e) {
				switch(e.keyCode) {
				case 40:
					var $curr = $(this).parents(".inputbox").find(".search li.active").next();
					if ($curr.html() != null) {
						$(this).parents(".inputbox").find(".search li").removeClass("active");
						$curr.addClass("active");
					}
					break;

				case 38:
					var $curr = $(this).parents(".inputbox").find(".search li.active").prev();
					if ($curr.html() != null) {
						$(this).parents(".inputbox").find(".search li").removeClass("active");
						$curr.addClass("active");
					}
					break;

				case 13:
					if ($(this).parents(".inputbox").find(".search ul").html() != "") {
						name = $(".search li.active").text().replace(/<.*>/, '');
						data = $(".search li.active a").attr("data");
						html = conv_inputbox_item(name, data);
						$(this).parents(".inputbox").find("span.address_list").append(html);
						$(this).parents(".inputbox").find(".search ul").html("");

						$(this).val("");
						$(this).parents(".inputbox").find(".search ul").hide();
					} else {
						email = $(this).val();
						if (validate(email, 'email')) {
							name = email;
							html = conv_inputbox_item(name, email);
							$(this).parents(".inputbox").find("span.address_list").append(html);
							$(this).val("");
						} else {
							ui_error("邮件格式错误");
							return false;
						}
					}
					break;

				default:
					var search = $(this).parents(".inputbox").find("div.search ul");
					if ($(this).val().length > 1) {
						$.getJSON("{:U('popup/json')}", {
							key : $(this).val()
						}, function(json) {
							if (json != "") {
								if (json.length > 0) {
									search.html("");
									$.each(json, function(i) {
										search.append('<li><a data="' + json[i].id + '" title="' + json[i].email + '">' + json[i].name + '&lt;' + json[i].email + '&gt;</a></li>');
									});
									search.children("li:first").addClass("active");
									search.show();
								}
							} else {
								search.html("");
								search.hide();
							}
						});
					} else {
						search.hide();
					}
				}
			});
		});
		function save() {
			$("#to").val("");
			$("#recever .address_list span").each(function() {
				$("#to").val($("#to").val() + $(this).find("b").text() + '|' + $(this).attr("data") + ";");
			});
			if ($("#to").val() == "") {
				ui_error("请选择收信人");
				$("#recever .letter").focus();
				return false;
			}
			sendForm("form_data", "{:U('save')}");
		}

		function popup_contact() {
			winopen("{:U('popup/message')}", 560, 470);
		}
	</script>

</block>